﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using T.FAS.Runtime.Base.Common;

namespace T.FAS.Runtime.ScheduleJob.SPI
{
    public class EnumUtil
    {
        public static Dictionary<object, object> EnumListDic<T>()
        {
            Dictionary<object, object> dicEnum = new Dictionary<object, object>();
            Type enumType = typeof(T);
            if (!enumType.IsEnum)
            {
                return dicEnum;
            }
            string[] fieldstrs = Enum.GetNames(enumType); //获取枚举字段数组
            foreach (var item in fieldstrs)
            {
                string description = string.Empty;
                var field = enumType.GetField(item);
                //object[] arr = field.GetCustomAttributes(typeof(DescriptionAttribute), true); //获取属性字段数组
                //if (arr != null && arr.Length > 0)
                //{
                //    description = ((DescriptionAttribute)arr[0]).Description;   //属性描述
                //}
                //else
                //{
                //    description = item;  //描述不存在取字段名称
                //}
                description = GetI18NTermValue(field);
                dicEnum.Add((int)Enum.Parse(enumType, item),description);  //不用枚举的value值作为字典key值的原因从枚举例子能看出来，其实这边应该判断他的值不存在，默认取字段名称
            }
            return dicEnum;
        }

        public static string GetEnumTermString(FieldInfo fieldInfo)
        {
            return $"Schema.Common.ScheduleJob.{fieldInfo.DeclaringType.Name}.{fieldInfo.Name}";
        }

        public static string GetI18NTermValue(FieldInfo fieldInfo)
        {
            try
            {
                var attr = (DescriptionAttribute)fieldInfo.GetCustomAttribute(typeof(DescriptionAttribute), true); //获取属性字段数组
                var originText = attr?.Description ?? fieldInfo.Name;
                //originText += " for test";
                var termCode = GetEnumTermString(fieldInfo);
                var translatedText = T.ServiceComponent.I18N.Service.TermService.Current.GetText(termCode, originText);
                return translatedText;
            }
            catch (Exception e)
            {
                throw new Exception($"Fail to Get Enum:{fieldInfo.DeclaringType.Name} Field:{fieldInfo.Name} Translated Text, err:{e}");
            }
        }

        public static List<string> GetTermCodeColle<T>()
        {
            List<string> termCodes = new List<string>();
            Type enumType = typeof(T);
            if (!enumType.IsEnum)
            {
                return default;
            }
            string[] fieldstrs = Enum.GetNames(enumType); //获取枚举字段数组
            foreach (var item in fieldstrs)
            {
                var field = enumType.GetField(item);
                var attr = (DescriptionAttribute)field.GetCustomAttribute(typeof(DescriptionAttribute), true); //获取属性字段数组
                var originText = attr?.Description ?? field.Name;
                var termCode = GetEnumTermString(field);
                termCodes.Add(termCode + ":" + originText);
            }
            return termCodes;
        }
    }
}
